package edu.upc.e402grupo02.e402grupo02.modelo.repository.impl;

import java.util.ArrayList;
import java.util.List;

import edu.upc.e402grupo02.e402grupo02.modelo.Cartelera;
import edu.upc.e402grupo02.e402grupo02.modelo.CarteleraId;
import edu.upc.e402grupo02.e402grupo02.modelo.Cine;
import edu.upc.e402grupo02.e402grupo02.repository.CarteleraRepository;
import edu.upc.e402grupo02.e402grupo02.repository.CineRepository;
import edu.upc.e402grupo02.e402grupo02.repository.PeliculaRepository;

public class CarteleraRepositoryImpl implements CarteleraRepository{

	private List<Cartelera> listCartelera;
	
	private CineRepository cineRepository;
	private PeliculaRepository peliculaRepository;
	
	
	public CarteleraRepositoryImpl() {
		cineRepository = new CineRepositoryImpl();
		peliculaRepository = new PeliculaRepositoryImpl();
		
		listCartelera = new ArrayList<Cartelera>();
		for(Cine c : CineRepositoryImpl.listaCines){

			if(c.getId().equals("1")){
				CarteleraId id = new CarteleraId("0430", peliculaRepository.findById("5"));
				Cartelera cartelera = new Cartelera(id, "Sala 1");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0530", peliculaRepository.findById("3"));
				cartelera = new Cartelera(id, "Sala 3");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0630", peliculaRepository.findById("1"));
				cartelera = new Cartelera(id, "Sala 4");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0830", peliculaRepository.findById("5"));
				cartelera = new Cartelera(id, "Sala 6");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("1030", peliculaRepository.findById("2"));
				cartelera = new Cartelera(id, "Sala 5");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
			}
			
			if(c.getId().equals("2")){
				CarteleraId id = new CarteleraId("0430", peliculaRepository.findById("3"));
				Cartelera cartelera = new Cartelera(id, "Sala 3");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0530", peliculaRepository.findById("5"));
				cartelera = new Cartelera(id, "Sala 4");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0630", peliculaRepository.findById("4"));
				cartelera = new Cartelera(id, "Sala 1");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0830", peliculaRepository.findById("1"));
				cartelera = new Cartelera(id, "Sala 2");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("1030", peliculaRepository.findById("2"));
				cartelera = new Cartelera(id, "Sala 7");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
			}
			
			if(c.getId().equals("3")){
				CarteleraId id = new CarteleraId("0430", peliculaRepository.findById("1"));
				Cartelera cartelera = new Cartelera(id, "Sala 1");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0530", peliculaRepository.findById("3"));
				cartelera = new Cartelera(id, "Sala 3");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0630", peliculaRepository.findById("1"));
				cartelera = new Cartelera(id, "Sala 4");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0830", peliculaRepository.findById("5"));
				cartelera = new Cartelera(id, "Sala 6");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("1030", peliculaRepository.findById("2"));
				cartelera = new Cartelera(id, "Sala 5");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
			}
			
			if(c.getId().equals("4")){
				CarteleraId id = new CarteleraId("0430", peliculaRepository.findById("2"));
				Cartelera cartelera = new Cartelera(id, "Sala 1");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0530", peliculaRepository.findById("5"));
				cartelera = new Cartelera(id, "Sala 3");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0630", peliculaRepository.findById("1"));
				cartelera = new Cartelera(id, "Sala 4");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("0830", peliculaRepository.findById("3"));
				cartelera = new Cartelera(id, "Sala 6");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
				
				id = new CarteleraId("1030", peliculaRepository.findById("2"));
				cartelera = new Cartelera(id, "Sala 5");
				cartelera.setCine(c);
				listCartelera.add(cartelera);
			}

			
		}
		
	}
	
	public List<Cartelera> findByCine(String cineId) {
		System.out.println("Cartelera por cine:");
		System.out.println("cineId: " + cineId);
		Cine cine = cineRepository.findById(cineId);
		List<Cartelera> carteleras = new ArrayList<Cartelera>();
		for(Cartelera c : listCartelera){
			if(c.getCine()==cine){
				carteleras.add(c);
				System.out.println(c.toString());
			}
				
		}
		
		System.out.println("");
		
		return carteleras;
	}

	public List<Cartelera> findPeliculaByCine(String peliculaId, String cineId) {
		System.out.println("Cartelera por Pelicula + Cine:");
		System.out.println("pelicua: " + peliculaId);
		System.out.println("cine: " + cineId);
		Cine cine = cineRepository.findById(cineId);
		List<Cartelera> carteleras = new ArrayList<Cartelera>();
		for(Cartelera c : listCartelera){
			if(c.getCine()==cine && c.getId().getPelicula().getId().equals(peliculaId)){
				carteleras.add(c);
				System.out.println(c.toString());
			}
				
		}
		
		System.out.println("");
		
		return carteleras;
	}

	public List<Cartelera> findByHorario(String horario) {
		System.out.println("Cartelera por horario:");
		System.out.println("horario: " + horario);
		List<Cartelera> carteleras = new ArrayList<Cartelera>();
		for(Cartelera c : listCartelera){
			if(c.getId().getHorario().equals(horario)){
				carteleras.add(c);
				
				System.out.println(c.toString());
			}
				
		}
		
		System.out.println("");
		
		return carteleras;
	}

	

}
